package scau.erps.view.system;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import scau.erps.app.Erps;
import scau.erps.entity.Score;
import scau.erps.service.ScoreService;
import scau.erps.service.impl.ScoreServiceImpl;

/**
 * 沙盘成绩面板
 * @author FYJ
 *
 */
public class SandResultsPanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private ScoreService scoreService = Erps.getIocInstance().get(
			ScoreServiceImpl.class);
	JPanel result = new JPanel();
	JPanel title = new JPanel();

	public SandResultsPanel() {
		// 获取小组排名
		Score[] getScore = getRanking();
		setOrder(getScore);
		// 排名框显示内容
		JLabel teamNoLabel = new JLabel("小组号");
		JLabel compositeScoreLabel = new JLabel("综合费用");
		JLabel ownerEquityLabel = new JLabel("所有者权益");
		JLabel bankruptcyYearLabel = new JLabel("破产年份");

		// 第一名
		JPanel first = new JPanel();
		first.setPreferredSize(new Dimension(800, 58));
		first.setLayout(new GridLayout(1, 4));
		first.setBorder(BorderFactory.createTitledBorder("第一名"));
		first.add(new JLabel("第" + getScore[0].getTeam_Id() + "组"));
		first.add(new JLabel("" + getScore[0].getScore()));
		first.add(new JLabel("" + getScore[0].getTotalEquity()));
		first.add(new JLabel("" + getScore[0].getBankrupt()));
		// 第二名
		JPanel second = new JPanel();
		second.setPreferredSize(new Dimension(800, 58));
		second.setLayout(new GridLayout(1, 4));
		second.setBorder(BorderFactory.createTitledBorder("第二名"));
		second.add(new JLabel("第" + getScore[1].getTeam_Id() + "组"));
		second.add(new JLabel("" + getScore[1].getScore()));
		second.add(new JLabel("" + getScore[1].getTotalEquity()));
		second.add(new JLabel("" + getScore[1].getBankrupt()));
		// 第三名
		JPanel third = new JPanel();
		third.setPreferredSize(new Dimension(800, 58));
		third.setLayout(new GridLayout(1, 4));
		third.setBorder(BorderFactory.createTitledBorder("第三名"));
		third.add(new JLabel("第" + getScore[2].getTeam_Id() + "组"));
		third.add(new JLabel("" + getScore[2].getScore()));
		third.add(new JLabel("" + getScore[2].getTotalEquity()));
		third.add(new JLabel("" + getScore[2].getBankrupt()));
		// 第四名
		JPanel fourth = new JPanel();
		fourth.setPreferredSize(new Dimension(800, 58));
		fourth.setLayout(new GridLayout(1, 4));
		fourth.setBorder(BorderFactory.createTitledBorder("第四名"));
		fourth.add(new JLabel("第" + getScore[3].getTeam_Id() + "组"));
		fourth.add(new JLabel("" + getScore[3].getScore()));
		fourth.add(new JLabel("" + getScore[3].getTotalEquity()));
		fourth.add(new JLabel("" + getScore[3].getBankrupt()));
		// 第五名
		JPanel fifth = new JPanel();
		fifth.setPreferredSize(new Dimension(800, 58));
		fifth.setLayout(new GridLayout(1, 4));
		fifth.setBorder(BorderFactory.createTitledBorder("第五名"));
		fifth.add(new JLabel("第" + getScore[4].getTeam_Id() + "组"));
		fifth.add(new JLabel("" + getScore[4].getScore()));
		fifth.add(new JLabel("" + getScore[4].getTotalEquity()));
		fifth.add(new JLabel("" + getScore[4].getBankrupt()));
		// 第六名
		JPanel sixth = new JPanel();
		sixth.setPreferredSize(new Dimension(800, 58));
		sixth.setLayout(new GridLayout(1, 4));
		sixth.setBorder(BorderFactory.createTitledBorder("第六名"));
		sixth.add(new JLabel("第" + getScore[5].getTeam_Id() + "组"));
		sixth.add(new JLabel("" + getScore[5].getScore()));
		sixth.add(new JLabel("" + getScore[5].getTotalEquity()));
		sixth.add(new JLabel("" + getScore[5].getBankrupt()));
		// 第七名
		JPanel seventh = new JPanel();
		seventh.setPreferredSize(new Dimension(800, 58));
		seventh.setLayout(new GridLayout(1, 4));
		seventh.setBorder(BorderFactory.createTitledBorder("第七名"));
		seventh.add(new JLabel("第" + getScore[6].getTeam_Id() + "组"));
		seventh.add(new JLabel("" + getScore[6].getScore()));
		seventh.add(new JLabel("" + getScore[6].getTotalEquity()));
		seventh.add(new JLabel("" + getScore[6].getBankrupt()));
		// 第八名
		JPanel eighth = new JPanel();
		eighth.setPreferredSize(new Dimension(800, 58));
		eighth.setLayout(new GridLayout(1, 4));
		eighth.setBorder(BorderFactory.createTitledBorder("第八名"));
		eighth.add(new JLabel("第" + getScore[7].getTeam_Id() + "组"));
		eighth.add(new JLabel("" + getScore[7].getScore()));
		eighth.add(new JLabel("" + getScore[7].getTotalEquity()));
		eighth.add(new JLabel("" + getScore[7].getBankrupt()));

		// 标题
		title.setPreferredSize(new Dimension(800, 58));
		title.setLayout(new GridLayout(1, 4));
		title.setBorder(BorderFactory.createTitledBorder("标题"));
		title.add(teamNoLabel);
		title.add(compositeScoreLabel);
		title.add(ownerEquityLabel);
		title.add(bankruptcyYearLabel);

		result.setLayout(new GridLayout(9, 1));
		result.add(title);
		result.add(first);
		result.add(second);
		result.add(third);
		result.add(fourth);
		result.add(fifth);
		result.add(sixth);
		result.add(seventh);
		result.add(eighth);
		this.add(result);
	}

	public Score[] getRanking() {//各小组排名情况
		Score[] aScore = new Score[8];// 未破产
		Score[] bScore = new Score[8];// 已破产
		List<Score> scoreList = scoreService.getScore(0);
		int i = 0, j = 0, z = 0;
		for (Score score : scoreList) {
			if (score.getBankrupt().equals("未破产")) {// 找出未破产小组
				aScore[i] = scoreList.get(z);
				i++;
			} else {// 找出已破产小组
				bScore[j] = scoreList.get(z);
				j++;
			}
			z++;
		}
		for (int a = 0; a < i - 1; a++) {// 未破产小组排名
			for (int b = 0; b < i - 1 - a; b++) {
				if (aScore[b].getScore() < aScore[b + 1].getScore()) {
					Score temp = aScore[b];
					aScore[b] = aScore[b + 1];
					aScore[b + 1] = temp;
				}
			}
		}
		for (int a = 0; a < j - 1; a++) {// 破产小组排名
			for (int b = 0; b < j - a - 1; b++) {
				if (littleYear(bScore[b].getBankrupt(),
						bScore[b + 1].getBankrupt())) {
					Score temp = bScore[b];
					bScore[b] = bScore[b + 1];
					bScore[b + 1] = temp;
				}
			}
		}
		//合并未破产和破产小组排名
		for (int d = i, e = 0; d < 8; d++, e++)
			aScore[d] = bScore[e];
		
		return aScore;

	}

	public boolean littleYear(String aYear, String bYear) {//比较破产时间先后
		if (aYear.charAt(1) < bYear.charAt(1))//比较年份
			return true;
		else if (aYear.charAt(1) == bYear.charAt(1)) {//比较季度
			if (aYear.charAt(3) < bYear.charAt(3))
				return true;
			else
				return false;
		} else
			return false;

	}
	public void setOrder(Score[] getScore){
		for(int i = 0;i<8;i++){
			List<Score> scoreList = scoreService.getScore(getScore[i].getTeam_Id());
			Score score = scoreList.get(0);
			score.setrank(i+1);
			scoreService.updateScore(score);
		}
	}
	
}
